package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.*;
import negModelExcep.*;

public class RuedaTest extends TestCase {

	Neumatico neum1;
	Llanta llanta1;
	Rueda rueda1,rueda2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		llanta1 = new Llanta (100, 25,165,new String ("llanta1"),new AlgoPesos(100),false,10,0.05);
		neum1 = new Neumatico(100,20,150,new String ("neum"),new AlgoPesos(100),false,10,100,10,3);
		rueda1 = new Rueda(llanta1,neum1,new String ("rueda1"),false);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","rueda");
		
		e.setAttribute("maxVU", ""+ rueda1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ rueda1.getVidaUtil());
		e.setAttribute("peso", ""+ rueda1.getPeso());
		e.setAttribute("aportevmax", ""+ rueda1.aportevmax());
		e.setAttribute("detalle", rueda1.getDetalle());
		e.setAttribute("precio", ""+(rueda1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+rueda1.esRemovible());
		e.setAttribute("performance", ""+rueda1.getPerformance());
		
		e.appendChild(llanta1.toXml(doc));
		e.appendChild(neum1.toXml(doc));		
		
	}

	public void testGastar() {
		try {
			rueda1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(198,rueda1.getVidaUtil(),0.1);
		
	}

	public void testFuncionar() {
		try {
			assertEquals(0.16,rueda1.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
	}

	public void testGetCoeficiente() {
		assertEquals(3,rueda1.getCoeficiente(),0.1);
	}

	public void testGetSuperficie() {
		assertEquals(1000,rueda1.getSuperficie(),0.1);
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=rueda1.toXml(doc);
		
		Fabrica unaFabrica=new Fabrica();
		Parte unaParte;
				
		NodeList nodos=e.getElementsByTagName("parte");
		for(int nc=0;nc<nodos.getLength();nc++){
			unaParte= unaFabrica.hacerParte(((Element)nodos.item(nc)).getAttribute("Clase"),(Element)nodos.item(nc));
			assertNotNull(unaParte);				
			}
			
		assertEquals(rueda1.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((rueda1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(rueda1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(rueda1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(rueda1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(rueda1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testRueda()throws Exception {
		
		Rueda rueda2=new Rueda(e);
				
		assertEquals(rueda2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((rueda2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(rueda2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(rueda2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(rueda2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(rueda2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));

		assertEquals(rueda2.getCoeficiente(),rueda1.getCoeficiente(),0.0);

		assertEquals(rueda2.getSuperficie(),rueda1.getSuperficie(),0.0);
		
	}

}
